package cn.edu.suse.epilepsy.service.impl;

import cn.edu.suse.epilepsy.common.BizException;
import cn.edu.suse.epilepsy.common.R;
import cn.edu.suse.epilepsy.entity.dto.PageDto;
import cn.edu.suse.epilepsy.entity.dto.PatientEduActivityDto;
import cn.edu.suse.epilepsy.entity.pojo.PatientEduActivity;
import cn.edu.suse.epilepsy.entity.vo.PageVo;
import cn.edu.suse.epilepsy.mapper.PatientEduActivityMapper;
import cn.edu.suse.epilepsy.service.PatientEduActivityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mockito.internal.util.collections.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class PatientEduActivityServiceImpl extends ServiceImpl<PatientEduActivityMapper, PatientEduActivity> implements PatientEduActivityService {
    @Autowired
    private PatientEduActivityMapper mapper;
    @Override
    public List<PatientEduActivity> fuzzQuery(PatientEduActivityDto dto) throws BizException {
        // {name:'张三',host:'李四'，signupStartTime:"2024-01-01",signupEndTime:"2024-0703",status:1}

     /*   select sys_user_name from tb_sys_user
        where (sys_user_name like ? OR sys_user_phone like ?)
        and
        created between ? and ?  */

//        SysUserQueryDto dto = new SysUserQueryDto();
//        dto.setField("张");
//        dto.setStartTime("2024-01-01");

        LambdaQueryWrapper<PatientEduActivity> wrapper = Wrappers.lambdaQuery(PatientEduActivity.class);

        // 控制被查询的字段,从wrapper中选择需要的字段
        wrapper.select(PatientEduActivity::getName,PatientEduActivity::getHost,PatientEduActivity::getStatus);

        // 判断field字段是否为空, 不为空才应该参与到条件构造
        if(!StringUtils.isEmpty(dto.getName())){
            // 多个条件之间默认是and的逻辑运算
            wrapper.like(PatientEduActivity::getName,dto.getName());
        }
        if(!StringUtils.isEmpty(dto.getHost())){
            // 多个条件之间默认是and的逻辑运算
            wrapper.like(PatientEduActivity::getHost,dto.getHost());
        }
        if(!StringUtils.isEmpty(dto.getStatus())){
            // 多个条件之间默认是and的逻辑运算
            wrapper.like(PatientEduActivity::getStatus,dto.getStatus());
        }
        if(!StringUtils.isEmpty(dto.getSignupStartTime())){
            // 多个条件之间默认是and的逻辑运算
            wrapper.like(PatientEduActivity::getSignupStartTime,dto.getSignupStartTime());
        }
        if(!StringUtils.isEmpty(dto.getSignupEndTime())){
            // 多个条件之间默认是and的逻辑运算
            wrapper.like(PatientEduActivity::getSignupEndTime,dto.getSignupEndTime());
        }
        if(mapper.selectList(wrapper)==null){
            throw new BizException("没有符合条件的患教活动");
        }
        // 根据条件查询结果
        return  mapper.selectList(wrapper);
        // mybatis plus 生成的sql:
        // 默认是根据wrapper条件的添加顺序生成条件(没有优先级)
        // 解决办法:
        // 1. 使用原生的mybatis(定义xml文件)
        // 2. 在业务中规定
//        Preparing: SELECT sys_user_id AS id,sys_user_name AS name,sys_user_phone AS phone,sys_user_pwd AS pwd,sys_user_avatar AS avatar,created,updated
//        FROM tb_sys_user
//        WHERE (sys_user_phone LIKE ? OR sys_user_name LIKE ? AND created >= ?)
    }

    @Override
    public R getAll(PageDto pageDto) {
        // 1.1.分页条件
        Page<PatientEduActivity> page = new Page<>(pageDto.getPage(), pageDto.getSize());
        QueryWrapper<PatientEduActivity> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(PatientEduActivity::getName,pageDto.getName())
                .like(PatientEduActivity::getHost,pageDto.getHost());
        if(!StringUtils.isEmpty(pageDto.getSignUpStartTime())){
            String startTime = pageDto.getSignUpStartTime();
            String startTimeDate = startTime.substring(0, startTime.indexOf("T"));
            wrapper.lambda().like(PatientEduActivity::getSignupStartTime, startTimeDate);

        }
        if(!StringUtils.isEmpty(pageDto.getSignUpEndTime())) {
            String endTime = pageDto.getSignUpEndTime();
            String endTimeDate = endTime.substring(0, endTime.indexOf("T"));
            wrapper.lambda().like(PatientEduActivity::getSignupStartTime, endTimeDate);

        }
        if(pageDto.getStatus() != -1){
            wrapper.lambda().eq(PatientEduActivity::getStatus,pageDto.getStatus());
        }

        Page<PatientEduActivity> patientEduActivityPage = this.baseMapper.selectPage(page, wrapper);
        PageVo pageVo = new PageVo();
        pageVo.setTotal(patientEduActivityPage.getTotal());
        pageVo.setTotalPage(patientEduActivityPage.getPages());
        pageVo.setList(patientEduActivityPage.getRecords());
        pageVo.setPage(pageDto.getPage());
        pageVo.setSize(pageDto.getSize());
        return R.success(pageVo);
    }

    @Override
    public R addPatient(PatientEduActivity patientEduActivity) {
        //设置默认参数
        patientEduActivity.setStatus(0);
        this.baseMapper.insert(patientEduActivity);
        return R.success();
    }
}
